<?php

namespace App\Http\Controllers\App;

use App\Http\Requests\App\CourseCompleteRequest;
use App\Http\Resources\App\CourseDetailResource;
use App\Models\Course;
use App\Models\Profile;
use App\Models\SkillCourse;
use App\Models\UserCourse;
use App\Models\UserSkill;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CourseController extends Controller
{
    public function detail(Request $request, Course $course)
    {
        return new CourseDetailResource($course->load(['users' => function ($query) use ($request) {
            return $query->where('user_id', $request->user()->id)->withPivot(UserCourse::OTHER_COLUMNS);
        }]));
    }

    //累计学习课程情况
    public function accumulate(Request $request, Course $course)
    {
        $add = 3;
        $gap = 1;

        $user = $course->users()->withPivot(UserCourse::OTHER_COLUMNS)->find($request->user()->id);
        if ($user) {
            $pivot = [];
            if (now()->diffInMinutes($user->pivot->updated_at) > $gap) {
                $pivot['learn_count'] = $user->pivot->learn_count + $add;
            } else {
                $pivot['learn_count'] = now()->diffInSeconds($user->pivot->updated_at) + $user->pivot->learn_count;
            }
            $pivot['updated_at'] = now();
            $course->users()->updateExistingPivot($request->user()->id, $pivot);
            return $pivot['learn_count'];
        } else {
            $userCourse = new UserCourse();
            $userCourse->course_id = $course->id;
            $userCourse->user_id = $request->user()->id;
            $userCourse->learn_count = $add;
            $userCourse->save();
            return $add;
        }
    }

//手动完成课程
    public function complete(CourseCompleteRequest $request, Course $course)
    {
//检测有无累计学习记录

        $user = $course->users()->withPivot(UserCourse::OTHER_COLUMNS)->find($request->user()->id);
        if (empty($user->pivot)) {
            abort(400, '未学习本课程');
        }

        if ($user->pivot->completed_at) {
            abort(400, '已完成过该课程');
        }

        //检测上一层有无学习

        $previousStudentStudySkillCourse = DB::select('select * from user_course join skill_course on skill_course.course_id = user_course.course_id where  json_contains(skill_course.next_course_id,?) and skill_course.skill_id = ? and user_course.user_id=?', [$course->id, $request->skill, $request->user()->id]);//学生学习的
        $previousSkillCourse = DB::select('select * from courses join skill_course on skill_course.course_id = courses.id where  json_contains(skill_course.next_course_id,?) and skill_course.skill_id = ? ', [$course->id, $request->skill]);//课程之前的
        $previousSkillCourseIds = collect($previousSkillCourse)->pluck('id')->filter();
        $previousStudentStudySkillCourse = collect($previousStudentStudySkillCourse)->pluck('course_id')->filter();
        $contains = $previousSkillCourseIds->diff($previousStudentStudySkillCourse->all())->isEmpty();

        if ($previousSkillCourse and !$contains) {
            abort(400, '未学习上节课程');
        }

        //保存用户课程
        $userCourse = $user->pivot;
        $userCourse->completed_at = now();
        $userCourse->save();
        //更新用户数据
        if (Profile::where('user_id', $request->user()->id)->exists()) {
            DB::update('UPDATE profiles set exp=exp+? , depth=GREATEST(depth,?) where user_id=?', [$course->exp, $course->depth, $request->user()->id]);

        } else {
            $userSkill = new Profile;
            $userSkill->user_id = $request->user()->id;
            $userSkill->exp = $course->exp;
            $userSkill->depth = $course->depth;
            $userSkill->save();
        }

        //更新用户技能学习时间
//        $sum = $course->users()->where('user_id', $request->user()->id)->sum('learn_count');
//        if (UserSkill::where('user_id', $request->user()->id)->where('skill_id', $request->skill)->exists()) {
////            DB::update('UPDATE profiles set exp=exp+? , depth=GREATEST(depth,?) where user_id=?', [$course->exp, $course->depth, $request->user()->id]);
////
//        } else {
//            $userSkill = new UserSkill;
//            $userSkill->user_id = $request->user()->id;
//            $userSkill->skill_id = $request->skill;
//            $userSkill->learn_count = 0;
////            $userSkill->com = $course->depth;
//            $userSkill->save();
//        }
    }
}
